//
//  FirstViewController.m
//  CustomDemo
//
//  Created by 于洪东 on 16/6/23.
//  Copyright © 2016年 于洪东. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"

#import "CustomNavigationController.h"
#import "CustomNaviBarSearchController.h"

@interface FirstViewController ()<CustomNaviBarSearchControllerDelegate>

@property (nonatomic,strong) CustomNaviBarSearchController *search;
@property (nonatomic,strong) NSMutableArray *searchKeyWordArray;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    [self initUI];
    [self initSearchUI];
    self.searchKeyWordArray = [NSMutableArray array];
    
}

//-(void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    
//    // 必须在此方法中被调用   但是感觉并不是很好。
//    [_search layoutSubViewConstrains];
//    
//}

#pragma mark - 响应方法
-(void)rightButtonClick:(UIButton *)sender{
    NSLog(@"子类中:ViewController");
    SecondViewController *second = [[SecondViewController alloc]init];
//    [self.navigationController pushViewController:second animated:YES];
//    [self presentViewController:second animated:YES completion:nil];
//    http://www.360doc.com/content/15/0702/11/11417867_482109349.shtml
    
    CustomNavigationController *nav = [[CustomNavigationController alloc]initWithRootViewController:second];
    [self presentViewController:nav animated:YES completion:nil];

}

#pragma mark - 私有方法
-(void)initUI{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(200, 200, 130, 30);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)initSearchUI{
    _search = [[CustomNaviBarSearchController alloc]initWithParentViewCtrl:self];
    _search.delegate = self;
    [_search resetPlaceHolder:@"去你妹的"];
    [_search setRecentKeyword:self.searchKeyWordArray];
    /**
     *  导航栏的类型，这句话必须要有
     */
    [_search showTempSearchCtrl];
}

#pragma mark - 协议方法   ------ CustomNaviBarSearchControllerDelegate

-(void)naviBarSearchCtrlCancel:(CustomNaviBarSearchController *)ctrl{
    NSLog(@"取消按钮点击的协议");
}

-(void)naviBarSearchCtrl:(CustomNaviBarSearchController *)ctrl searchKeyword:(NSString *)strKeyword{
    NSLog(@".....%@",strKeyword);
    [self.searchKeyWordArray addObject:strKeyword];
    [_search setRecentKeyword:self.searchKeyWordArray];
    
}

-(void)naviBarSearchCtrlClearKeywordRecord:(CustomNaviBarSearchController *)ctrl{
    [_search setRecentKeyword:nil];
    [self.searchKeyWordArray removeAllObjects];
}





@end
